package com.lion.seaman.server.drivers;

import com.lion.seaman.common.Constants;
import com.lion.seaman.worker.jobs.SimpleJob;
import com.lion.seaman.worker.thread.ThreadPoolManager;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;

public class JobDriver implements Job {
    private String group = Constants.DEFAULT_JOB_GROUP;
    private String name;
    private String cron;
    private Map<String, Object> data;
    private String scriptPath;
    private String description;


    public JobDriver() {

    }

    public JobDriver(String name, String cron, String scriptPath) {
        this(name, cron, scriptPath, "");
    }

    public JobDriver(String name, String cron, String scriptPath, String description) {
        Assert.isTrue(name != null && !name.trim().equals(""), "job name must not be null");
        Assert.isTrue(cron != null && !cron.trim().equals(""), "job cron must not be null");
        Assert.isTrue(scriptPath != null && !scriptPath.trim().equals(""), "job scriptPath must not be null");
        this.name = name;
        this.cron = cron;
        this.scriptPath = scriptPath;
        this.description = description;
        addDefaultData();
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
        SimpleJob job = new SimpleJob((String) jobDataMap.get(Constants.DEFAULT_JOB_NAME_KEY), (String) jobDataMap.get(Constants.DEFAULT_JOB_SCRIPT_KEY));
        job.putAllData(jobDataMap);
        ThreadPoolManager.MANAGER.getTaskExecutor().execute(job);
    }

    public void addDefaultData() {
        if (this.data == null) {
            this.data = new HashMap<>();
        }
        this.data.put(Constants.DEFAULT_JOB_NAME_KEY, this.name);
        this.data.put(Constants.DEFAULT_JOB_SCRIPT_KEY, this.scriptPath);
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
        addDefaultData();
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getScriptPath() {
        return scriptPath;
    }

    public void setScriptPath(String scriptPath) {
        this.scriptPath = scriptPath;
        addDefaultData();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        addDefaultData();
    }

    public String getCron() {
        return cron;
    }

    public void setCron(String cron) {
        this.cron = cron;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
